package server;

import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Scanner;

public class Style 
{
	private String styleText = null;
	
	private static Style instance = null;
	
	//private InputStream[] iconFiles = new InputStream[9];
	HashMap<String, String> resMap = new HashMap<String, String>();
	
	private Style()
	{
		for(int i = 0; i < 9; i++)
		{
			//try 
			{
				//iconFiles[i] = getClass().getResourceAsStream("/resources/icons/icon-" + (i+1) + ".png");
				System.out.println("Icon loaded " + i);
				resMap.put("icon-" + (i+1) + ".png", "/resources/icons/icon-" + (i+1) + ".png");
			} 
			//catch (URISyntaxException e) 
			//{
			//	System.out.println("Couldn't load icon no " + i);
			//} 
		}
		
		//try 
		//{
			//InputStream icoLight = getClass().getResourceAsStream("/resources/icons/icon-light.png");
			resMap.put("icon-light.png", "/resources/icons/icon-light.png");
		//} 
		//catch (URISyntaxException e) { }
		
		//try 
		//{
			//File icoLight = new File(getClass().getResource("/resources/icons/icon-dark.png").toURI());
			resMap.put("icon-dark.png", "/resources/icons/icon-dark.png");
		//} 
		//catch (URISyntaxException e) { }
		
		//try 
		//{
			//File icoLight = new File(getClass().getResource("/resources/icons/icon-dark-32.png").toURI());
			resMap.put("icon-dark-32.png", "/resources/icons/icon-dark-32.png");
		//} 
		//catch (URISyntaxException e) { }
	}
	
	public static Style getInstance()
	{
		if(instance == null)
			instance = new Style();
		return instance;
	}
	
	public InputStream getFileStream(String path)
	{
		if(path.startsWith("/res/"))
			path = path.substring(5, path.length());
		if(path.startsWith("res/"))
			path = path.substring(4, path.length());
		
		String resName = null;
		
		if(path.equals("/favicon.ico"))
		{
			resName = "/resources/icon-dark.ico";
		}
		else
		{
			resName = resMap.get(path);
			if(resName == null)
				return null;
		}
		
		return getClass().getResourceAsStream(resName);
	}
	
	public void dumpHeader(PrintWriter pw)
	{
		pw.println("<html>");
		pw.println("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\">");
		pw.println("<meta charset=\"utf-8\">");
		pw.println("<meta content='width=device-width, initial-scale=0.9, maximum-scale=1.0, user-scalable=0' name='viewport' />");
		pw.println("<head>");
		pw.println("<title>InstaDir share</title>");
		dumpStylesheet(pw);
		pw.println("</title>");
		pw.println("<body><div class=\"container\">");
	}
	
	public void dumpFooter(PrintWriter pw)
	{
		pw.println("</div></body></html>");
	}
	
	public void dumpStylesheet(PrintWriter pw)
	{
		if(styleText == null)
		{
			//try 
			{
				InputStream is = getClass().getResourceAsStream("/resources/instastyle.css");
				StringBuilder sb = new StringBuilder();
				Scanner s = new Scanner(is);
				while(s.hasNextLine())
				{
					sb.append(s.nextLine());
					sb.append('\n');
				}
				s.close();
				styleText = sb.toString();
			} 
			
		}
		
		pw.println("<style>");
		pw.println(styleText);
		pw.println("</style>");
	}
}
